//
//  MoblieLoginViewController.m
//  huashiedu
//
//  Created by oceans on 2018/7/29.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "MoblieLoginViewController.h"
#import "Utility.h"

@interface MoblieLoginViewController ()

@property (strong, nonatomic) UITextField *username;
@property (strong, nonatomic) UITextField *password;
@property (strong, nonatomic) UIButton *getCodeButton;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) double timertime;
@end

@implementation MoblieLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"登录";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(0, 0, 44, 44);
    [close setImage:[UIImage imageNamed:@"chacha"] forState:UIControlStateNormal];
    [close addTarget:self action:@selector(toClose) forControlEvents:UIControlEventTouchUpInside];
    UIView *closeView = [[UIView alloc] initWithFrame:close.bounds];
    [closeView addSubview:close];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeView];
    
    UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    imagev.center = CGPointMake(ScreenWidth * 0.5, 87 + XNavigationHeight);
    imagev.image = [UIImage imageNamed:@"loginicon"];
    [self.view addSubview:imagev];
    
    self.username = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(imagev.frame) + 46, ScreenWidth - 62, 35)];
    _username.placeholder = @"请输入手机号";
    _username.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_username];
    
    CALayer *topline = [[CALayer alloc] init];
    topline.frame = CGRectMake(30, CGRectGetMaxY(_username.frame) + 6.5, ScreenWidth - 60, 0.5);
    topline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:topline];
    
    self.password = [[UITextField alloc] initWithFrame:CGRectMake(31, CGRectGetMaxY(topline.frame) + 17, ScreenWidth - 62, 35)];
    _password.placeholder = @"请输入验证码";
    [self.view addSubview:_password];
    
    CALayer *botline = [[CALayer alloc] init];
    botline.frame = CGRectMake(30, CGRectGetMaxY(_password.frame) + 6.5, ScreenWidth - 60, 0.5);
    botline.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:botline];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(31, CGRectGetMaxY(botline.frame) + 35.0, ScreenWidth - 62, 50);
    login.backgroundColor = COLOR(253, 119, 77, 1);
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(toLogin) forControlEvents:UIControlEventTouchUpInside];
    login.layer.cornerRadius = 3.0;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
    
    UIButton *center = [UIButton buttonWithType:UIButtonTypeCustom];
    center.frame = CGRectMake(0, 0, 100, 30);
    center.center = CGPointMake(ScreenWidth * 0.5, CGRectGetMaxY(login.frame) + 35.0);
    [center setTitleColor:COLOR(115, 117, 116, 1) forState:UIControlStateNormal];
    center.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [center setTitle:@"帐号密码登录" forState:UIControlStateNormal];
    [center addTarget:self action:@selector(toPasswordLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:center];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 136.0, 20)];
    title.center = CGPointMake(ScreenWidth * 0.5, ScreenHeight - XNavigationHeight - 100);
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:14.0];
    title.textColor = COLOR(165, 165, 165, 1);
    title.text = @"其他方式登录";
    [self.view addSubview:title];
    
    CALayer *leftLine = [[CALayer alloc] init];
    leftLine.frame = CGRectMake(10, ScreenHeight - XNavigationHeight - 100, (ScreenWidth - 136 - 20) * 0.5, 0.5);
    leftLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:leftLine];
    
    CALayer *rightLine = [[CALayer alloc] init];
    rightLine.frame = CGRectMake((ScreenWidth + 136) * 0.5, ScreenHeight - XNavigationHeight - 100, (ScreenWidth - 136 - 20) * 0.5, 0.5);
    rightLine.backgroundColor = [UIColor colorWithRed:226/255.0 green:226/255.0 blue:226/255.0 alpha:1.0].CGColor;
    [self.view.layer addSublayer:rightLine];
    
    UIButton *wechat = [UIButton buttonWithType:UIButtonTypeCustom];
    wechat.frame = CGRectMake(0, 0, 47, 47);
    wechat.center = CGPointMake(ScreenWidth * 0.5 - 50, ScreenHeight - XNavigationHeight - 50);
    [wechat setImage:[UIImage imageNamed:@"wechatlogin"] forState:UIControlStateNormal];
    [wechat addTarget:self action:@selector(loginWithWechat) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wechat];
    
    UIButton *qq = [UIButton buttonWithType:UIButtonTypeCustom];
    qq.frame = CGRectMake(0, 0, 47, 47);
    qq.center = CGPointMake(ScreenWidth * 0.5 + 50, ScreenHeight - XNavigationHeight - 50);
    [qq setImage:[UIImage imageNamed:@"qqlogin"] forState:UIControlStateNormal];
    [qq addTarget:self action:@selector(loginWithQQ) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:qq];
    
    self.getCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _getCodeButton.frame = CGRectMake(0, 0, 113, 33);
    [_getCodeButton setTitleColor:COLOR(151, 151, 151, 1) forState:UIControlStateNormal];
    _getCodeButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getCodeButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
    _password.rightView = _getCodeButton;
    _password.rightViewMode = UITextFieldViewModeAlways;
}

- (void)toClose {
    [self.timer invalidate];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)getCode {
    
    if ([Utility checkMobile:_username.text]) {
        [self.view endEditing:YES];
        _timertime = 60;
        [self startTimer];
        __weak typeof(self) weakself = self;
        [[UserModel shared] getCodeWithPhoneNumber:_username.text scope:@"signin" successBlock:nil failedBlock:^{
            [weakself stopTimer];
        }];
    }
}

- (void)startTimer {
    
    _getCodeButton.enabled = NO;
    [_getCodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

- (void)timerMethod {
    
    if (_timertime <= 0) {
        [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
        [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeButton.enabled = YES;
        [self.timer invalidate];
        return;
    }
    _timertime = _timertime - 1;
    [_getCodeButton setTitle:[NSString stringWithFormat:@"(%.0f)秒后可重发", _timertime] forState:UIControlStateNormal];
}

- (void)stopTimer {
    [_getCodeButton setTitleColor:COLOR(235, 86, 0, 1) forState:UIControlStateNormal];
    [_getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _getCodeButton.enabled = YES;
    [self.timer invalidate];
}

- (void)toLogin {
    
    if ([Utility checkMobile:_username.text] && _password.text.length > 0) {
        
        [self.view endEditing:YES];
        __weak typeof(self) weakself = self;
        [[UserModel shared] loginWithCode:_password.text username:_username.text successBlock:^{
            [weakself toClose];
        }];
    }
}

- (void)toPasswordLogin {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loginWithWechat {
    __weak typeof(self) weakself = self;
    [[UserModel shared] loginWithThirdParty:TPL_WECHAT successBlock:^{
        [weakself toClose];
    } failedBlock:^{
        
    }];
}

- (void)loginWithQQ {
    __weak typeof(self) weakself = self;
    [[UserModel shared] loginWithThirdParty:TPL_QQ successBlock:^{
        [weakself toClose];
    } failedBlock:^{
        
    }];
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
